package com.ctsi.framework.mybatis.support;

import com.ctsi.common.$;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.InvocationTargetException;

/**
 * @author: zhangjw
 * @Date: 2018/11/15 12:49
 * @Description:
 */
public class VoTemplate {

    private static final Logger logger = LoggerFactory.getLogger(VoTemplate.class);

    /**
     * 自定义回调函数
     * @param vo
     * @param <T>
     * @return
     */
    public <T> T execute(VoCallback<T> vo) {
        return vo.doInVo();
    }

    /**
     * 默认的回调方法，将v的属性复制给t，并将t返回
     * @param t 回调对象
     * @param v 目标对象
     * @param <T>
     * @param <V>
     * @return
     */
    public <T,V> T defaultExcute(final T t,final V v){
        return this.execute(new VoCallback<T>() {
            @Override
            public T doInVo() {
                if (v==null) {
                    return null;
                } else {
                    try {
                        $.bean.copyProperties(t, v);
                        return t;
                    } catch (InvocationTargetException | IllegalAccessException e) {
                        logger.error("convert EntityVO occur ERROR {}", e);
                    }
                    return null;
                }
            }
        });
    }

}
